//
//  NavigationViewController.m
//  Fruit
//
//  Created by kai xie on 15/8/7.
//  Copyright (c) 2015年 kai xie. All rights reserved.
//

#import "NavigationViewController.h"
#import "UIBarButtonItem+Extension.h"

@interface NavigationViewController ()

@end

@implementation NavigationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = (id)self;
    
    // 隐藏navigationbar底部线条
    CALayer *botBorder = [CALayer layer];
    botBorder.frame = CGRectMake(0.0f, 44.0f,kSCREEN_WIDTH, 0.5f);
    botBorder.backgroundColor = FTColor.CGColor;
    [self.navigationBar.layer addSublayer:botBorder];
    
    __weak typeof (self) weakSelf = self;
    
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

        self.interactivePopGestureRecognizer.delegate = (id)weakSelf;
    }
}

+ (void)initialize
{
    UINavigationBar *appearance = [UINavigationBar appearance];
    
    // 设置导航栏字体颜色为白色
    [appearance setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];// [UIFont boldSystemFontOfSize:17]; 默认大小
    
    [appearance setBarStyle:UIBarStyleDefault];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // 在push的时候关闭手势, 这样就不用担心会激活滑动
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    
    if(self.viewControllers.count > 0){
        //如果现在push的不是栈底控制器
        viewController.hidesBottomBarWhenPushed = YES;
        // 设置导航栏按钮
        UIBarButtonItem *btn = [UIBarButtonItem itemWithImageName:@"backArrow" highImageName:@"backArrow" target:self action:@selector(back)];
        
        UIBarButtonItem *logoBtn = [UIBarButtonItem itemWithImageName:@"small_md" highImageName:@"small_md" target:self action:nil];
        
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:[UIButton buttonWithType:UIButtonTypeCustom]];
        
        viewController.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:btn, item, logoBtn, nil];
    }
    
    [super pushViewController:viewController animated:animated];
    
    if ([super respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        super.interactivePopGestureRecognizer.delegate = nil;
    }
}


- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // 加载完成下一个viewController之后需要激活滑动手势
    if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
    
    // 在rootController下滑动的时候, 在想push到下一个页面会没有反应, 界面卡死在那了, 所以还需要在上述方法中加入
    if (navigationController.viewControllers.count == 1) {
        navigationController.interactivePopGestureRecognizer.enabled = NO;
        navigationController.interactivePopGestureRecognizer.delegate = nil;
    }
}

- (void)back
{
    [self popViewControllerAnimated:YES];
}

- (void)more
{
    [self popToRootViewControllerAnimated:YES];
}

@end
